layui.config({
    base: "/module/",
    debug: true
}).extend({
	oss: "libs/aliy/oss",
    dtree: "libs/dtree/dtree",
    tableMerge: 'libs/tableMerge/tableMerge',
	autocomplete: "libs/autocomplete/autocomplete",
	formSelects: "libs/formSelects/formSelects-v4.all",
	treeSelect: 'libs/treeSelect/treeSelect',
	xmSelect: "libs/xm-select/xm-select",
	treeTable: "libs/treeTable/treeTable",
})


function Taller(){
	this.key = '';
	this.el = {},
	this.api =  {},
	this.module = ['sdHttp', 'sdUtils', 'sdFormat', 'sdTable', 'sdDialog', 'sdForm', 'sysApplication', 'globalConstant'];
	this.script = [];
	this.css = [];
	this.filter = [];
	this.render = [];
}
function soda(){
	return new Taller();
}
Taller.prototype = {
	_module(_module){
		if(_module){
			this.module = this.module.concat(_module);
		}
		return this;
	},
	_page(_el){
		let el = {};
		$.each(['view', 'table', 'form', 'search'], (i, d) => {
			el[d] = _el + '_' + d;
		})
		this.el = el;
		this.key = _el;
		return this;
	},
	_api(_api){
		let  api = {};
		api['list'] = _api + '/list';
		api['crud'] = _api;
		this.api = api;
		return this;
	},
	_script(_script){
		$.extend(this.script, _script);
		return this;
	},
	_css(_css){
		$.extend(this.css, _css);
		return this;
	},
	_query(_query){
		let query = [];
		$.each(_query, (i, d) => {
			let q = d.split('^');
			let o = {
				field: q[0],
				opt: q[1],
				val: q[2]
			}
			query.push(o)
		})
		
		this.query = query;
		return this;
	},
	_orderBy(_orderBy){
		let orderBy = [];
		$.each(_orderBy, (i, d) => {
			let q = d.split('^');
			let o = {
				field: q[0],
				orderBy: q[1]
			}
			orderBy.push(o)
		})
		this.orderBy = orderBy;
		return this;
	},
	_run(fn){
		layui.use(this.module, () => {
			
			// 组件配置
			$.each(this.module, (i, d) => {
				this[d] = layui[d];
			})
			
			// 动态载入JS
			let script = new this['sdUtils'].script();
			$.each(this.script, (i, d) => {
				script.set(d);
			})
			// 动态载入css
			$.each(this.css, (i, d) => {
				layui.link(d)
			})
			
			// 条件过滤
			this.filter = {
				vo: this.query
			}
			
			// 初始化form对象
			this.templateForm = {
				id: this.el['form']
			}
			// 初始化table对象
			
			this.templateTable = {
				id: this.el['table'],
				url: this.api['list'],
				where: {
					where: this['sdFormat'].query(this.query),
					orderBy: this['sdFormat'].orderBy(this.orderBy)
				},
				search: {
					filter: this.el['search'],
				}
			}

			// 初始化Dialog弹出层
			this.templateDialog = {
				form(data){
					
				},
				delete(data){
					
				}
			}
			
			// 去除多余参数
			delete this.css
			delete this.module
			delete this.script
			delete this.query
			delete this.orderBy
			
			fn(this)
		})
	}
}